package com.fqh.controller;

import com.fqh.entity.JsonResult;
import com.fqh.entity.Login;
import com.fqh.service.UserService;
import com.fqh.util.CodeUtil;
import com.fqh.util.SMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Scope(value = "prototype")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("login.do")
    public JsonResult checkByUsernameAndPassword(@RequestParam("telephone") String telephone, @RequestParam("pwd") String pwd, HttpServletRequest request) {
        List<Login> list = userService.checkByUsernameAndPassword(telephone, pwd);
        if (list != null && list.size() != 0) {
            HttpSession session = request.getSession();
            session.setAttribute("user", list.get(0));
            return new JsonResult("成功", true);
        } else {
            return new JsonResult("用户名或密码错误", false);
        }
    }

    @RequestMapping("findUsername.do")
    public JsonResult findUsername(@RequestParam("username") String username) {
        List<Login> list = userService.findUsername(username);
        if (list.size() == 0) {
            return new JsonResult("√", true);
        } else {
            return new JsonResult("被占用", false);
        }
    }

    @RequestMapping("findTelephone.do")
    public JsonResult findTelephone(@RequestParam("telephone") String telephone) {
        List<Login> list = userService.findTelephone(telephone);
        if (list.size() == 0) {
            return new JsonResult("√", true);
        } else {
            return new JsonResult("已存在", false);
        }
    }

    @RequestMapping("sendCode.do")
    public JsonResult sendCode(@RequestParam("phone") String phone, HttpServletRequest request) {
        String code = CodeUtil.getCode();
        HttpSession session = request.getSession();
        session.setAttribute("code", code);
        SMSUtil.sendSMS(phone, code);
        return new JsonResult("", true);
    }

    @PostMapping("registerUser.do")
    public JsonResult registerUser(HttpServletRequest request, String username, String password, String telephone, String code) {
        HttpSession session = request.getSession();
        String realCode = (String) session.getAttribute("code");
        if (realCode.equalsIgnoreCase(code)) {
            userService.addUser(username, password, telephone);
            return new JsonResult("注册成功", true);
        } else {
            return new JsonResult("验证码不正确!", false);
        }
    }

    @RequestMapping("findPwd.do")
    public Boolean findPwd(String pwd, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Login user = (Login) session.getAttribute("user");
        String password = user.getPassword();
        if (password.equals(pwd)) {
            return true;
        }
        return false;
    }

    @RequestMapping("changePwd.do")
    public JsonResult changePwd(@RequestParam("confirmPwd") String pwd, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Login user = (Login) session.getAttribute("user");
        try {
            userService.changePwd(user.getLoginId(), pwd);
            return new JsonResult("修改成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("修改失败", false);
        }
    }

    @RequestMapping("exit.do")
    public void exit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().invalidate();
        response.sendRedirect("/Boheyuan_Portal_war_exploded/The login.html");
    }
}
